package com.plugin.planilha;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import jxl.Workbook;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class PlanilhaXLS {
	
	private static final PlanilhaXLS instance =  new PlanilhaXLS();

//	private static HashMap<Chave, int[]> dadosPlanilhaTotal = new HashMap<Chave, int[]>();
//	private static HashMap<Chave, int[]> dadosPlanilhaporClasse = new HashMap<Chave, int[]>();
//	private static WritableSheet RepeticaoListSheet;
	private static ArrayList<ISheet> sheets;
	private static WritableWorkbook workbook;

//	private static Integer RepeticaoListSheetSize = 1;

	private PlanilhaXLS() {
		sheets = new ArrayList<ISheet>();
	}

	public static void criarArquivo() throws Exception {

		String fileName = "/Users/theocritomm/Desenvolvimento/workspace/plugin_JCF/src/Metricas.xls";
		workbook = Workbook.createWorkbook(new File(fileName));
//		WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
//				WritableFont.BOLD);
//		WritableCellFormat cf = new WritableCellFormat(wf);
//		cf.setWrap(true);

//		criaCabecalhoRepeticaoListSheet(cf);
	}

	public static void criaRepeticaoListSheet(HashMap<String, Map<String, Integer[]>> map) throws Exception {
		ISheet repeticaoSheet = new AnaliseListSheet(workbook, map);
		sheets.add(repeticaoSheet);
	}
	
	public static void criaToarraySetSheet(HashMap<String, Map<String, Integer>> map) throws Exception {
		ISheet toarraySheet = new AnaliseSetSheet(workbook, map);
		sheets.add(toarraySheet);
	}
	
	public static void criaMapSheet(HashMap<String, Map<String, Integer[]>> map) throws Exception {
		ISheet mapSheet = new AnaliseMapSheet(workbook, map);
		sheets.add(mapSheet);
	}
	
	public static void criaDefasadaHashtableSheet(HashMap<String, HashSet<String>> map_defasado, HashMap<String, HashSet<String>> map_usado) throws Exception {
		ISheet defasadaHashtableSheet = new AnaliseDefasadaHashtableSheet(workbook, map_defasado, map_usado);
		sheets.add(defasadaHashtableSheet);
	}
	
	public static void criaDefasadaVectorSheet(HashMap<String, HashSet<String>> map_defasado, HashMap<String, HashSet<String>> map_usado) throws Exception {
		ISheet defasadaVectorSheet = new AnaliseDefasadaVectorSheet(workbook, map_defasado, map_usado);
		sheets.add(defasadaVectorSheet);
	}
	
	public static void criaDefasadaStackSheet(HashMap<String, HashSet<String>> map_defasado, HashMap<String, HashSet<String>> map_usado) throws Exception {
		ISheet defasadaStackSheet = new AnaliseDefasadaStackSheet(workbook, map_defasado, map_usado);
		sheets.add(defasadaStackSheet);
	}
	
	public static void criaArraylistSheet(HashMap<String, Map<String, Integer[]>> map) throws Exception {
		ISheet arraylistSheet = new AnaliseArraylistSheet(workbook, map);
		sheets.add(arraylistSheet);
	}
	
	public static void criaLinkedlistSheet(HashMap<String, Map<String, Integer[]>> map) throws Exception {
		ISheet linkedlistSheet = new AnaliseLinkedlistSheet(workbook, map);
		sheets.add(linkedlistSheet);
	}
	
	public static void criaVectorSheet(HashMap<String, Map<String, Integer[]>> map) throws Exception {
		ISheet vectorSheet = new AnaliseVectorSheet(workbook, map);
		sheets.add(vectorSheet);
	}	
	
	public static void criaAllCabecalhos() throws Exception{
		WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
				WritableFont.BOLD);
		WritableCellFormat cf = new WritableCellFormat(wf);
		cf.setWrap(true);
		
		for (ISheet sheet : sheets) {
			sheet.criaCabecalho(cf);
		}
	}
	
	public static void addAllDados() throws Exception{
		for(ISheet sheet: sheets){
			sheet.addDados();
		}
	}

	public static void finalizarArquivo() throws IOException, WriteException {
		workbook.write();
		workbook.close();
	}

	public static PlanilhaXLS getInstance(){
		return instance;
	}
//	private static void criaCabecalhoRepeticaoListSheet(WritableCellFormat cf)
//			throws WriteException, RowsExceededException {
//
//		Label label = new Label(0, 0, "Variavel", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(1, 0, "ADDs depois de CONTAINS", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(2, 0, "ADDs", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(3, 0, "Taxa de comparacao(%)", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(5, 0, "Total ADDS", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(6, 0, "Total ADDS depois de CONTAINS", cf);
//		RepeticaoListSheet.addCell(label);
//
//		label = new Label(7, 0,
//				"Taxa de comparacao total de ADDs depois de CONTAINS em List:",
//				cf);
//		RepeticaoListSheet.addCell(label);
//
//		Formula f = new Formula(6, 1, "SUM(B2:B1048576)");
//		RepeticaoListSheet.addCell(f);
//
//		f = new Formula(5, 1, "SUM(C2:C1048576)");
//		RepeticaoListSheet.addCell(f);
//
//		f = new Formula(7, 1, "(100*G2)/F2");
//		RepeticaoListSheet.addCell(f);
//		
//		RepeticaoListSheet.mergeCells(9, 0, 12, 0);
//		label = new Label(11, 0,
//				"LIST",
//				cf);
//		RepeticaoListSheet.addCell(label);
//	}
//
//	public static void adicionaDadosRepeticaoListSheet(
//			Map<String, Map<String, Integer[]>> map)
//			throws RowsExceededException, WriteException {
//		Label label;
//		jxl.write.Number n;
//
//		for (String ch : map.keySet()) {
//			if (map.get(ch).size() > 0) {
//				for (String variavel : map.get(ch).keySet()) {
//					int add_contains = map.get(ch).get(variavel)[0];
//					int add = map.get(ch).get(variavel)[1];
//
//					label = new Label(0, RepeticaoListSheetSize, variavel);
//					RepeticaoListSheet.addCell(label);
//
//					n = new jxl.write.Number(1, RepeticaoListSheetSize,
//							add_contains);
//					RepeticaoListSheet.addCell(n);
//
//					n = new jxl.write.Number(2, RepeticaoListSheetSize, add);
//					RepeticaoListSheet.addCell(n);
//
//					if (add != 0) {
//						int taxa_comparacao = (add_contains * 100) / add;
//						n = new jxl.write.Number(3, RepeticaoListSheetSize,
//								taxa_comparacao);
//						RepeticaoListSheet.addCell(n);
//					} else {
//						label = new Label(3, RepeticaoListSheetSize, "null");
//						RepeticaoListSheet.addCell(label);
//					}
//					RepeticaoListSheetSize++;
//				}
//			}
//		}
//	}

//	public static HashMap<Chave, int[]> getDadosPlanilha() {
//		return dadosPlanilhaTotal;
//	}
//
//	public static void setDadosPlanilha(HashMap dadosPlanilha) {
//		PlanilhaXLS.dadosPlanilhaporClasse = dadosPlanilha;
//	}
//
//	public static HashMap<Chave, int[]> getDadosPlanilhaClasse() {
//		return dadosPlanilhaporClasse;
//	}
//
//	public static void adicionarDados() throws WriteException,
//			RowsExceededException {
//		Label label;
//		Object[] chaves = dadosPlanilhaporClasse.keySet().toArray();
//		int indice = dadosPlanilhaTotal.keySet().size() + 1;
//
//		for (int i = 0; i < chaves.length; i++) {
//			Chave chave = (Chave) chaves[i];
//			String variavel = chave.getVariavel();
//			String tipo = chave.getTipo();
//
//			int[] valores = (int[]) dadosPlanilhaporClasse.get(chave);
//
//			label = new Label(0, indice, variavel);
//			RepeticaoListSheet.addCell(label);
//
//			label = new Label(1, indice, tipo);
//			RepeticaoListSheet.addCell(label);
//
//			int coluna = 2;
//			for (int j = 0; j < valores.length; j++) {
//				label = new Label(coluna++, indice, String.valueOf(valores[j]));
//				RepeticaoListSheet.addCell(label);
//			}
//			indice++;
//		}
//		dadosPlanilhaTotal.putAll(dadosPlanilhaporClasse);
//	}
}
